package cn.itsource.org.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 事务acid
 *  1 原子性
 *  2 一致性：回滚-----在一个事务内报错看它是否回滚
 *  3 隔离性
 *  4 持续性
 */
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    //department的添加和修改和默认添加和修改不一样，以自己实现为主----重写
    @Override
    public void add(Department department) {
        departmentMapper.save(department);
        this.update(department);

    }

    @Override
    public void update(Department department) {
        // 判断是否是等级部门 追加自己就ok
        if (department.getParent()==null){
            department.setDirPath("/"+department.getId());
        }else{
            // 父亲dirpath+/自己id
            Long parentId = department.getParent().getId();
            String parentDirPath = departmentMapper.loadById(parentId).getDirPath();
            String dirPath = parentDirPath+"/"+department.getId();
            department.setDirPath(dirPath);
        }
        departmentMapper.update(department);
    }

    @Override
    public void bulkDel(List<Long> ids) {
        departmentMapper.bulkRemove(ids);
    }

    @Override
    public List<Department> queryDeptTree() {
        return departmentMapper.loadDeptTree();
    }
}
